<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Publish extends MY_Controller
{

    public function index()
    {
        $this->rent();
    }

    /**
     * 发布租房/出租房信息
     */
    public function rent()
    {
        $data = array(
            'title' => '发布租房信息',
        );
        $this->get_common_section( $data );

        if ( chk_permission( 'publishrent' ) )
        {
            //具有发布权限
            $rentForm = Modules::run( 'postForm/rent' );
            $data['rentForm'] = $rentForm;
        }
        else
        {
            //提示注册成功
            $messageData = array(
                'title' => '抱歉，非法操作',
                'message' => '您没有权限执行该操作',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['rentForm'] = $messageBox;
        }

        $this->load->view( 'publish/rent', $data );
    }

    /**
     * 租房信息接收并处理
     */
    public function rentSubmit()
    {
        if ( chk_permission( 'publishrent' ) && $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            $postRentData = Modules::run( 'postForm/rentSubmit' );
            if ( $postRentData )
            {
                if ( isset($_FILES['image'] ) && $_FILES['image'] )
                {
                    //上传图片
                    $this->load->library( 'upload', get_upload_config() );
                    if ( !$this->upload->do_upload('image') )
                    {
                        $rentForm = Modules::run( 'postForm/rent' );
                        $data = array(
                            'title' => '发布租房信息',
                            'error' => $this->upload->display_errors(),
                            'rentForm' => $rentForm,
                        );
                        $this->get_common_section( $data );
                        $this->load->view( 'publish/rent', $data );
                        exit;
                    }
                    else
                    {
                        //获取上传数据
                        $uploadData = $this->upload->data();
                        $postRentData['upload'] = $uploadData;
                    }
                }

                $this->model_post_trans_begin();
                //插入post表
                $postId = $this->model_post_add( $postRentData );
                //如果有图片
                if ( isset( $postRentData['upload'] ) )
                {
                    $attachId = $this->model_attachment_add( $postRentData['upload'] );
                    $this->model_postattachment_add( array( 'postid' => $postId, 'attachid' => $attachId ) );
                }
                if ( $this->model_post_trans_status() === FALSE )
                {
                    $this->model_post_trans_rollback();
                    $rentForm = Modules::run( 'postForm/rent' );
                    $data = array(
                        'title' => '发布租房信息',
                        'rentForm' => $rentForm,
                        'error' => '系统出现错误，请重试看看',
                    );
                    $this->load->view( 'publish/rent', $data );
                }
                else
                {
                    $this->model_post_trans_commit();
                    //提示注册成功
                    $messageData = array(
                        'title' => '发布成功',
                        'message' => "租房信息发布成功，4秒后跳转回首页",
                        'autoRedirect' => true,
                        'autoRedirectTimeOut' => 4,
                        'autoRedirectURL' => config_item( 'base_url' ),
                    );
                    $messageBox = Modules::run( 'messageBox', $messageData );
                    $data = array(
                        'title' => '发布租房信息',
                        'rentForm' => $messageBox,
                    );
                    $this->get_common_section( $data );
                    $this->load->view( 'publish/rent', $data );
                }
            }
            else
            {
                $this->rent();
            }
        }
        else
        {
            $this->rent();
        }
    }
    
    /**
     * 发布出售信息
     */
    public function sell()
    {
        $data = array(
            'title' => '发布售房信息',
        );
        $this->get_common_section( $data );

        if ( chk_permission( 'publishbuy' ) )
        {
            //具有发布权限
            $rentForm = Modules::run( 'postForm/sell' );
            $data['rentForm'] = $rentForm;
        }
        else
        {
            //提示注册成功
            $messageData = array(
                'title' => '抱歉，非法操作',
                'message' => '您没有权限执行该操作',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => config_item( 'base_url' ),
            );
            $messageBox = Modules::run( 'messageBox', $messageData );
            $data['rentForm'] = $messageBox;
        }

        $this->load->view( 'publish/rent', $data );
    }

    /**
     * 出售信息接收并处理
     */
    public function sellSubmit()
    {
        if ( chk_permission( 'publishbuy' ) && $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            $postRentData = Modules::run( 'postForm/sellSubmit' );
            if ( $postRentData )
            {
                if ( isset($_FILES['image'] ) && $_FILES['image'] )
                {
                    //上传图片
                    $this->load->library( 'upload', get_upload_config() );
                    if ( !$this->upload->do_upload('image') )
                    {
                        $rentForm = Modules::run( 'postForm/sell' );
                        $data = array(
                            'title' => '发布售房信息',
                            'error' => $this->upload->display_errors(),
                            'rentForm' => $rentForm,
                        );
                        $this->get_common_section( $data );
                        $this->load->view( 'publish/rent', $data );
                        exit;
                    }
                    else
                    {
                        //获取上传数据
                        $uploadData = $this->upload->data();
                        $postRentData['upload'] = $uploadData;
                    }
                }

                $this->model_post_trans_begin();
                //插入post表
                $postId = $this->model_post_add( $postRentData );
                //如果有图片
                if ( isset( $postRentData['upload'] ) )
                {
                    $attachId = $this->model_attachment_add( $postRentData['upload'] );
                    $this->model_postattachment_add( array( 'postid' => $postId, 'attachid' => $attachId ) );
                }
                if ( $this->model_post_trans_status() === FALSE )
                {
                    $this->model_post_trans_rollback();
                    $rentForm = Modules::run( 'postForm/rent' );
                    $data = array(
                        'title' => '发布售房信息',
                        'rentForm' => $rentForm,
                        'error' => '系统出现错误，请重试看看',
                    );
                    $this->load->view( 'publish/rent', $data );
                }
                else
                {
                    $this->model_post_trans_commit();
                    //提示注册成功
                    $messageData = array(
                        'title' => '发布成功',
                        'message' => "售房信息发布成功，4秒后跳转回首页",
                        'autoRedirect' => true,
                        'autoRedirectTimeOut' => 4,
                        'autoRedirectURL' => config_item( 'base_url' ),
                    );
                    $messageBox = Modules::run( 'messageBox', $messageData );
                    $data = array(
                        'title' => '发布售房信息',
                        'rentForm' => $messageBox,
                    );
                    $this->get_common_section( $data );
                    $this->load->view( 'publish/rent', $data );
                }
            }
            else
            {
                $this->rent();
            }
        }
        else
        {
            $this->rent();
        }
    }

}